from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from pprint import pprint

from common.database import get_db
from core.response import resp_200, resp_400
from core.logger import logger

from app.audit.handlers.audit_api_record_handler import AuditApiRecordHandler
from app.audit.handlers.audit_event_handler import AuditEventHandler
from app.audit.handlers.audit_inspection_handler import AuditInspectionHandler
from core.security import permission_checker

from app.audit.schemas import *


router = APIRouter()



#### api调用记录操作 ####
@router.get('/api_record/', summary="获取api调用记录信息, 根据表字段-模糊匹配", description="审计")
async def get_audit_api_record_search(field: str = Query("all", description="查询的字段名称"), value: str = Query("", description="字段值"),
                              page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                              db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_api_record_handler = AuditApiRecordHandler()
    result = audit_api_record_handler.get_audit_api_record_search(db, page, per_page, field, value)
    if not result:
        message = "get audit api record search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/api_record/match/', summary="获取api调用记录信息, 根据表字段-完全匹配", description="审计")
async def get_audit_api_record_match(field: str = Query("all", description="查询的字段名称"), value: str = Query("", description="字段值"),
                              page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                              db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_api_record_handler = AuditApiRecordHandler()
    result = audit_api_record_handler.get_audit_api_record_match(db, page, per_page, field, value)
    if not result:
        message = "get audit api record match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.delete('/api_record/', summary="删除api调用记录", description="审计")
async def delete_audit_api_record(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_api_record_handler = AuditApiRecordHandler()
    result = audit_api_record_handler.delete_audit_api_record(db, data.dict())
    if not result:
        message = "delete audit api record failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()










#### 审计事件调用记录操作 ####
@router.get('/event/', summary="获取审计事件记录信息, 根据表字段-模糊匹配", description="审计")
async def get_audit_event_search(field: str = Query("all", description="查询的字段名称"), value: str = Query("", description="字段值"),
                              page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                              db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_event_handler = AuditEventHandler()
    result = audit_event_handler.get_audit_event_search(db, page, per_page, field, value)
    if not result:
        message = "get audit event search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/event/match/', summary="获取审计事件记录信息, 根据表字段-完全匹配", description="审计")
async def get_audit_event_match(field: str = Query("all", description="查询的字段名称"), value: str = Query("", description="字段值"),
                              page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                              db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_event_handler = AuditEventHandler()
    result = audit_event_handler.get_audit_event_match(db, page, per_page, field, value)
    if not result:
        message = "get audit event match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)



@router.post('/event/', summary="增加审计事件记录", description="审计")
async def add_audit_event(data: PostAuditEvent, db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_event_handler = AuditEventHandler()
    result = audit_event_handler.add_audit_event(db, data.dict())
    if not result:
        message = "delete audit event failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()




@router.delete('/event/', summary="删除审计事件记录", description="审计")
async def delete_audit_event(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_event_handler = AuditEventHandler()
    result = audit_event_handler.delete_audit_event(db, data.dict())
    if not result:
        message = "delete audit event failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()










#### 巡检事件调用记录操作 ####
@router.get('/inspection/', summary="获取巡检记录信息, 根据表字段-模糊匹配", description="审计")
async def get_audit_inspection_search(field: str = Query("all", description="查询的字段名称"), value: str = Query("", description="字段值"),
                              page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                              db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_inspection_handler = AuditInspectionHandler()
    result = audit_inspection_handler.get_audit_inspection_search(db, page, per_page, field, value)
    if not result:
        message = "get audit inspection search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/inspection/match/', summary="获取巡检记录信息, 根据表字段-完全匹配", description="审计")
async def get_audit_inspection_match(field: str = Query("all", description="查询的字段名称"), value: str = Query("", description="字段值"),
                              page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                              db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_inspection_handler = AuditInspectionHandler()
    result = audit_inspection_handler.get_audit_inspection_match(db, page, per_page, field, value)
    if not result:
        message = "get audit inspection match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)



@router.post('/inspection/', summary="增加巡检事件记录", description="审计")
async def add_audit_inspection(data: PostAuditInspection, db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_inspection_handler = AuditInspectionHandler()
    result = audit_inspection_handler.add_audit_inspection(db, data.dict())
    if not result:
        message = "add audit inspection failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()




@router.delete('/inspection/', summary="删除巡检事件记录", description="审计")
async def delete_audit_inspection(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    audit_inspection_handler = AuditInspectionHandler()
    result = audit_inspection_handler.delete_audit_inspection(db, data.dict())
    if not result:
        message = "delete inspection inspection failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()
